package com.xy.mainp.home.fragment.widget;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.xy.img.GlideUtil;
import com.xy.mainp.R;
import com.xy.mainp.databinding.ViewBenifitItemBinding;
import com.xy.mainp.model.home.android.HomeResultEntity;
import com.xy.mainp.utils.TimeUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * Created by John on 2016/11/7.
 */

public class ItemBenifitView extends FrameLayout implements View.OnClickListener {

    private ViewBenifitItemBinding binding;
    private HomeResultEntity data;

    public ItemBenifitView(Context context) {
        this(context,null);
    }

    public ItemBenifitView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ItemBenifitView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.view_benifit_item, this, true);
        binding.ivGifBenifit.setOnClickListener(this);
        binding.ivBenifitCollect.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view == binding.ivBenifitCollect){

        }else if (view == binding.ivGifBenifit){

        }
    }

    public void setData(HomeResultEntity data) {
        this.data = data;
        try {
            binding.setHomeResultEntity(data);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
            long time = dateFormat.parse(data.getPublishedAt()).getTime();
            binding.tvBenifitTime.setText(TimeUtil.friendlyTime(time));
            if (data.getType().equalsIgnoreCase("福利")){
                if (data.getUrl() != null){
                    GlideUtil.loadImage(data.getUrl(),binding.ivGifBenifit);
                    binding.ivGifBenifit.setVisibility(VISIBLE);
                }else {
                    binding.ivGifBenifit.setVisibility(GONE);
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
